package com.green.common.fusioncharts;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * 单条图形
 * 绘制柱状图、饼图
 * @author yinyuqiao
 * @time 2013-1-7 上午11:23:00
 */
public class SingleChart {
	private StringBuffer buffer;
	private Map<String, Points[]> map;
	private List<ChartXLabel> label;
	private double maxX;
	private double minX;
	private double maxY;
	private double minY;
	
	private String caption = null; // 图的名字
	private String yAxisName = null; // y轴名称 此处中文不能正确显示
	private String xAxisName = null; // x轴名称
	private String showValues = null; // 显示形式
	private String decimalPrecision = null; //小数点精确位数
	
	
	public Map<String, Points[]> getMap() {
		return map;
	}
	public void setMap(Map<String, Points[]> map) {
		this.map = map;
	}
	public List<ChartXLabel> getLabel() {
		return label;
	}
	public void setLabel(List<ChartXLabel> label) {
		this.label = label;
	}
	public SingleChart(){
		
	}
	/**
	 * 构造函数--初始化
	 * @param caption
	 * @param xAxisName
	 * @param yAxisName
	 * @param showValues
	 */
	public SingleChart(String caption,String xAxisName,String yAxisName,String showValues,String decimalPrecision){
		this.caption=caption;
		this.xAxisName=xAxisName;
		this.yAxisName=yAxisName;
		this.showValues=showValues;
		this.decimalPrecision=decimalPrecision;
	}
	/**
	 * 拼接字符串，取得绘图数据
	 * @return
	 */
	public String getColumnChartStr(){
		buffer = new StringBuffer();
		this.setXY();
		try{
			buffer.append("<chart caption=\""
					+caption
					+ "\" xAxisName=\""
					+ xAxisName
					+ "\" decimalPrecision=\""
					+ decimalPrecision
					+ "\" yAxisName=\""
					+ yAxisName
					+ "\" showValues=\""
					+ showValues +"\">");
		}catch(Exception e){
			e.printStackTrace();
		}
//		buffer.append("<set label=\""
//				+"first"
//				+"\" value=\""
//				+262 +"\" />");
		//设值
		for (ChartXLabel obj : label) {
			buffer.append("<set label=\"");
			buffer.append(obj.getName());
			buffer.append("\" value=\"");
			buffer.append(obj.getValue());
			buffer.append("\" />");
		}
		buffer.append("</chart>");
//		System.out.println(buffer.toString());
		return buffer.toString();
	}
	/**
	 * 设置X轴Y轴坐标
	 * 初始化绘图面板
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public void setXY() { // 设置x y 轴的坐标
		minX = minY = 0.0;
		maxX = maxY = 1.0;
		if (map != null) {
			for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, Points[]> e = (Map.Entry<String, Points[]>) it.next();
				
				Points[] list = (Points[]) e.getValue();
				@SuppressWarnings("unused")
				String name  =  e.getKey();
				
				for (int i = 0; i < list.length; i++) {
					double tempX = list[i].getX();
					double tempY = list[i].getY();
					if (tempX > maxX) {
						maxX = tempX;
					}else if(tempX < minX){
						minX = tempX;
					}
					if (tempY > maxY) {
						maxY = list[i].getY();
					}else if(tempY < minY){
						minY = tempY;
					}
				}
			}
		} else {
			
		}
		maxY = Math.rint(maxY*4/3);
		minY = Math.rint(minY*3);
		@SuppressWarnings("unused")
		double x = maxX - minX ;
		@SuppressWarnings("unused")
		double y = maxY - minY ;
	}
	
}
